package com.ruoyi.web.controller.system;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.http.ResponseEntity;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.domain.DroneMission;
import com.ruoyi.system.service.IDroneMissionService;

/**
 * 无人机任务管理
 */
@RestController
@RequestMapping("/system/drone")
public class DroneMissionController extends BaseController {

    @Autowired
    private IDroneMissionService droneMissionService;

    @Autowired
    private RestTemplate restTemplate;

    /**
     * 连接无人机
     */
    @PostMapping("/connect")
    public AjaxResult connectDrone() {
        try {
            boolean result = droneMissionService.connectDrone();
            return result ? AjaxResult.success("无人机连接成功") : AjaxResult.error("无人机连接失败");
        } catch (Exception e) {
            return AjaxResult.error("连接失败: " + e.getMessage());
        }
    }

    /**
     * 断开无人机连接
     */
    @PostMapping("/disconnect")
    public AjaxResult disconnectDrone() {
        try {
            boolean result = droneMissionService.disconnectDrone();
            return result ? AjaxResult.success("无人机断开成功") : AjaxResult.error("无人机断开失败");
        } catch (Exception e) {
            return AjaxResult.error("断开失败: " + e.getMessage());
        }
    }

    /**
     * 开始巡田任务
     */
    @PostMapping("/mission/start")
    public AjaxResult startMission(@RequestBody Map<String, Object> missionParams) {
        try {
            String missionId = droneMissionService.startMission(missionParams);
            return AjaxResult.success("任务启动成功", missionId);
        } catch (Exception e) {
            return AjaxResult.error("任务启动失败: " + e.getMessage());
        }
    }

    /**
     * 停止巡田任务
     */
    @PostMapping("/mission/stop")
    public AjaxResult stopMission() {
        try {
            Map<String, Object> missionReport = droneMissionService.stopMission();
            return AjaxResult.success("任务停止成功", missionReport);
        } catch (Exception e) {
            return AjaxResult.error("任务停止失败: " + e.getMessage());
        }
    }

    /**
     * 获取飞行适宜性评估
     */
    @GetMapping("/flight/suitability")
    public AjaxResult getFlightSuitability(String location) {
        try {
            Map<String, Object> suitability = droneMissionService.getFlightSuitability(location);
            return AjaxResult.success(suitability);
        } catch (Exception e) {
            return AjaxResult.error("获取飞行适宜性评估失败: " + e.getMessage());
        }
    }

    /**
     * 获取环境报告
     */
    @GetMapping("/environmental/report")
    public AjaxResult getEnvironmentalReport(String missionId) {
        try {
            Map<String, Object> report = droneMissionService.getEnvironmentalReport(missionId);
            return AjaxResult.success(report);
        } catch (Exception e) {
            return AjaxResult.error("获取环境报告失败: " + e.getMessage());
        }
    }

    // 在DroneMissionController.java中添加

    @GetMapping("/images/info")
    public AjaxResult getImagesInfo() {
        try {
            String url = "http://172.16.15.128:5002/drone/images/info";

            // 使用RestTemplate调用无人机服务
            ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
            JSONObject result = JSON.parseObject(response.getBody());

            if (result.getBooleanValue("success")) {
                return AjaxResult.success("获取图片信息成功", result.getJSONObject("data"));
            } else {
                return AjaxResult.error("获取图片信息失败: " + result.getString("message"));
            }
        } catch (Exception e) {
            return AjaxResult.error("获取图片信息异常: " + e.getMessage());
        }
    }

    /**
     * 获取任务状态
     */
    @GetMapping("/mission/status")
    public AjaxResult getMissionStatus() {
        try {
            Map<String, Object> status = droneMissionService.getMissionStatus();
            return AjaxResult.success(status);
        } catch (Exception e) {
            return AjaxResult.error("获取状态失败: " + e.getMessage());
        }
    }

    /**
     * 获取无人机状态
     */
    @GetMapping("/status")
    public AjaxResult getDroneStatus() {
        try {
            Map<String, Object> status = droneMissionService.getDroneStatus();
            return AjaxResult.success(status);
        } catch (Exception e) {
            return AjaxResult.error("获取状态失败: " + e.getMessage());
        }
    }

    /**
     * 获取任务历史列表
     */
    @GetMapping("/mission/history")
    public TableDataInfo missionHistory(DroneMission droneMission) {
        startPage();
        List<DroneMission> list = droneMissionService.selectDroneMissionList(droneMission);
        return getDataTable(list);
    }

    /**
     * 获取任务报告
     */
    @GetMapping("/mission/report/{missionId}")
    public AjaxResult getMissionReport(String missionId) {
        try {
            Map<String, Object> report = droneMissionService.getMissionReport(missionId);
            return AjaxResult.success(report);
        } catch (Exception e) {
            return AjaxResult.error("获取报告失败: " + e.getMessage());
        }
    }
}
